//
//  MGViewController.m
//  Pods-MGUIKit_Example
//
//  Created by XXX on 2018/7/28.
//

#import "PTViewController.h"
#import "UIImage+YYAdd.h"
#import "YYCGUtilities.h"
#import <Masonry/Masonry.h>

#import <objc/runtime.h>

#define NAVIGATIONBAR_HEIGHT 44

@interface PTViewController ()<UIGestureRecognizerDelegate>

@end

@implementation PTViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor themeBackgroundColor];

    //去除返回按钮的文案
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backButtonItem];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [self setNeedsStatusBarAppearanceUpdate];
    
    self.view.clipsToBounds = YES;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    //创建自定义导航栏以及返回按钮
    if (self.useCustomNavigationBar) {
        [self.view addSubview:self.mgNavigationBar];
        [self.mgNavigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            CGFloat height = 64 + iPhoneXYOffset;
            make.height.mas_equalTo(height);
        }];
        
        NSArray *viewController = self.navigationController.viewControllers;
        if (viewController.count > 1 && self != viewController[0]) {
            self.mgNavigationBar.backButton = nil;
        } else {
            [self.mgNavigationBar.backButton removeFromSuperview];
        }
    }
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self setMGNavigationBarHidden:self.nagigationBarHidden animated:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.nagigationBarHidden && !self.navigationController.isNavigationBarHidden) {
        [self setMGNavigationBarHidden:NO animated:animated];
    } else {
        [self setMGNavigationBarHidden:self.nagigationBarHidden animated:animated];
    }
}

- (void)setupSystemRightBarButtonItem:(NSString *)title {
    UIButton *rightBtn = [[UIButton alloc]init];
    rightBtn.frame = CGRectMake(0, 0, 50, 44);
    rightBtn.titleLabel.font = [UIFont regularPingFangFontOfSize:15];
    rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightBtn setTitleColor:[UIColor whiteColor] forState:0];
    [rightBtn setTitle:title forState:0];
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)setupSystemLeftBarButtonWithImage:(NSString *)icon {
    // 导航栏右侧按钮
    UIButton* settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    settingButton.frame = CGRectMake(0, 0, 30, NAVIGATIONBAR_HEIGHT + 14);
    settingButton.titleLabel.font = [UIFont regularPingFangFontOfSize:14.0f];
    settingButton.centerY = 22;
    settingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [settingButton setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [settingButton addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingButton];
}

- (void)setupSystemRightBarButtonWithImage:(NSString *)icon {
    // 导航栏右侧按钮
    UIButton* settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    settingButton.frame = CGRectMake(0, 0, 30, NAVIGATIONBAR_HEIGHT + 14);
    settingButton.titleLabel.font = [UIFont regularPingFangFontOfSize:14.0f];
    settingButton.centerY = 22;
    settingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [settingButton setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [settingButton addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingButton];
}


- (void)rightBtnAction {
    
}

- (void)leftBtnAction {
    
}

//保持自定义导航栏在最顶部
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    if (self.useCustomNavigationBar && self.mgNavigationBar) {
        [self.view bringSubviewToFront:self.mgNavigationBar];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.useCustomNavigationBar && self.mgNavigationBar) {
        [self.view bringSubviewToFront:self.mgNavigationBar];
    }
}

#pragma mark 自定义导航栏相关

- (void)setRightBarItemWithButtonTitle:(NSString *)buttonTitle target:(id)target action:(SEL)action {
    if (self.useCustomNavigationBar) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tintColor = [UIColor whiteColor];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitle:buttonTitle forState:UIControlStateNormal];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        self.mgNavigationBar.rightButton = button;
    }
}

- (void)setRightBarItemWithImage:(UIImage *)image target:(id)target action:(SEL)action {
    if (self.useCustomNavigationBar) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tintColor = [UIColor whiteColor];
        [button setImage:image forState:UIControlStateNormal];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        self.mgNavigationBar.rightButton = button;
    }
}

- (void)setRightBarItemWithButton:(UIButton *)button {
    if (self.useCustomNavigationBar) {
        self.mgNavigationBar.rightButton = button;
    }
}

- (void)setRightBarItemWithButtons:(NSArray *)buttons {
    if (self.useCustomNavigationBar) {
        self.mgNavigationBar.rightButtons = buttons;
    }
}

- (void)setLeftBarItemWithButtons:(NSArray *)buttons {
    if (self.useCustomNavigationBar) {
        self.mgNavigationBar.leftButtons = buttons;
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
    if (self.navigationController && self.navigationController.viewControllers.count == 1) {
        return NO;
    }
    return YES;
}

#pragma mark - 系统相关方法

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

#pragma mark  getter & setter

- (PTNavigationBar *)mgNavigationBar {
    if (self.useCustomNavigationBar && !_mgNavigationBar) {
        _mgNavigationBar = [[PTNavigationBar alloc] init];
        _mgNavigationBar.backgroundColor = [UIColor clearColor];
        _mgNavigationBar.viewController = self;
    }
    return _mgNavigationBar;
}

- (void)setTitle:(NSString *)title {
    if (self.useCustomNavigationBar) {
        self.mgNavigationBar.title = title;
    } else {
        [super setTitle:title];
    }
}

- (NSString *)title {
    if (self.useCustomNavigationBar) {
        return self.mgNavigationBar.title;
    } else {
        return [super title];
    }
}

- (void)setUseCustomNavigationBar:(BOOL)useMGNavigationBar {
    _useCustomNavigationBar = useMGNavigationBar;
    self.nagigationBarHidden = YES;
}

- (void)setMGNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    if (!hidden && self.nagigationBarHidden) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController setNavigationBarHidden:hidden animated:NO];
        });
    } else {
        [self.navigationController setNavigationBarHidden:hidden animated:animated];
    }
}

//设置自定义导航栏透明度
- (void)setNavigationBarAlpha:(CGFloat)alpha {
    if (self.useCustomNavigationBar) {
        [self.mgNavigationBar setValue:@(alpha) forKeyPath:@"backgroundView.alpha"];
    }
}

#pragma mark 其他方法
- (void)popSelf {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
